﻿using System;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Input;
using SnakeGame.Models;
using SnakeGame.Views;

namespace SnakeGame.ViewModels
{
    public class QuestionWindowViewModel : NotifyPropertyChanged
    {
        public ICommand CorrectCommand { get; set; }
        public ICommand StartCommand { get; set; }
        public ICommand WrongCommand { get; set; }
        private MainFrame _mainFrame;
        private DispatcherTimer _timer;
        private int _timeout;
        public int Timeout
        {
            get { return _timeout; }
            set
            {
                _timeout = value;
                OnPropertyChanged("Timeout");
            }
        }

        private Visibility _isTimeoutEnd;
        public Visibility TimeoutEnd
        {
            get { return _isTimeoutEnd; }
            set
            {
                _isTimeoutEnd = value;
                OnPropertyChanged("TimeoutEnd");
            }
        }

        private Visibility _showCountdown;
        public Visibility ShowCountdown
        {
            get { return _showCountdown; }
            set
            {
                _showCountdown = value;
                OnPropertyChanged("ShowCountdown");
            }
        }

        public string Win { get; set; }
        public string Lose { get; set; }
        public string Quest { get; set; }
        public string Answer { get; set; }
        public bool IsWin { get; set; }

        public event EventHandler RequestClose;

        protected void OnRequestClose()
        {
            if (_timer != null)
            {
                _timer.Stop();
            }
            if (RequestClose != null)
            {
                RequestClose(this, EventArgs.Empty);
            }
        }

        public QuestionWindowViewModel()
        {
            CorrectCommand = new RelayCommand(OnCorrectCommand);
            WrongCommand = new RelayCommand(OnWrongCommand);
            StartCommand = new RelayCommand(OnStartCommand);
        }

        private void OnStartCommand(object obj)
        {
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Start();
            ShowCountdown = Visibility.Visible;
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Timeout -= 1;
            if (Timeout <= 0)
            {
                _timer.Stop();
                Timeout = 0;
                TimeoutEnd = Visibility.Visible;
                ShowCountdown = Visibility.Hidden;
            }
        }

        private void OnWrongCommand(object obj)
        {
            IsWin = false;
            OnRequestClose();
        }

        private void OnCorrectCommand(object obj)
        {
            IsWin = true;
            OnRequestClose();
        }

        public void Initialize(MainFrame mf, Question q, int playerIndex)
        {
            _mainFrame = mf;
            Quest = q.Quest;
            Timeout = q.Timeout;
            Win = Convert.ToString(q.Win, 10);
            Lose = Convert.ToString(q.Lose, 10);
            Answer = q.Answer;

            TimeoutEnd = Visibility.Hidden;
            ShowCountdown = Visibility.Hidden;

        }

    }
}
